Daily Exercise 21

Ecosystem Science and Sustainability 330

Author

Leona Myers

library(tsibble)
Warning: package 'tsibble' was built under R version 4.4.3
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'
The following objects are masked from 'package:base':

    intersect, setdiff, union
library(feasts)
Warning: package 'feasts' was built under R version 4.4.3
Loading required package: fabletools
Warning: package 'fabletools' was built under R version 4.4.3
library(plotly)
Warning: package 'plotly' was built under R version 4.4.3
Loading required package: ggplot2

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(ggplot2)
library(dataRetrieval)
Warning: package 'dataRetrieval' was built under R version 4.4.3
library(lubridate)

Attaching package: 'lubridate'
The following object is masked from 'package:tsibble':

    interval
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Data Retrieval

library(dataRetrieval)
library(dplyr)
library(tsibble)

# Example: Cache la Poudre River at Mouth (USGS site 06752260)
poudre_flow <- readNWISdv(siteNumber = "06752260",    # Download data from USGS for site 06752260
                          parameterCd = "00060",      # Parameter code 00060 = discharge in cfs)
                          startDate = "2013-01-01",   # Set the start date
                          endDate = "2023-12-31") |>  # Set the end date
  renameNWISColumns() |>                              # Rename columns to standard names (e.g., "Flow", "Date")
  mutate(Date = yearmonth(Date)) |>                   # Convert daily Date values into a year-month format (e.g., "2023 Jan")
  group_by(Date) |>                                   # Group the data by the new monthly Date
  summarise(Flow = mean(Flow))                       # Calculate the average daily flow for each month
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31

Convert to tsibble

poudre_ts <- poudre_flow |>
  as_tsibble(index = Date)

Plotting the time series

flow_plot <- ggplot(poudre_ts, aes(x = Date, y = Flow)) +
  geom_line(color = "steelblue") +
  labs(title = "Monthly Average Flow - Cache la Poudre River",
       x = "Date", y = "Flow (cfs)") +
  theme_minimal()
flow_plot

ggplotly(flow_plot)

Subseries

poudre_ts |>
  gg_subseries(Flow)

This graph shows strong seasonal variation, the highest streamflows consistently occur during May and June with peaks above 2000 cubic feet per second while flows are lower in the winter months. Seasons are defined by calendar months. The subseries are montly time series broken out by month across year.

Decompose

library(fabletools) 

decomp <- poudre_ts |>
  model(STL(Flow ~ season(window = "periodic"))) |>
  components()

autoplot(decomp)

There is a gradual decline in flow over the decade which could be tied to climate trends. The yearly pattern is very consistent with peaks in May and June. There are some peaks and dips in the remainder.